﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DALFactory;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;
    using WMS6.DALFactory;

    public class DeliveryOrderBLL
    {
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private List<string> dataBaseID;
        private static readonly IDeliveryOrderDA deliveryOrderDA = DataAccess.CreateDeliveryOrderDA();
        private static readonly IDeliveryOrderDetailDA deliveryOrderDetailDA = DataAccess.CreateDeliveryOrderDetailDA();
        private static readonly IOrderDetailDA orderDetailDA = DataAccess.CreateOrderDetailDA();
        private static readonly IOrdersDA ordersDA = DataAccess.CreateOrdersDA();
        private PubParasInfo pubParasInfo;
        private static readonly ISysSettingDA sysSettingDA = DataAccess.CreateSysSettingDA();
        private static readonly IWaveDA waveDA = DataAccess.CreateWaveDA();
        private static readonly IWaveDetailDA waveDetailDA = DataAccess.CreateWaveDetailDA();

        public DeliveryOrderBLL()
        {
            this.pubParasInfo = null;
        }

        public DeliveryOrderBLL(string currentWH)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public DeliveryOrderBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        private DeliveryOrderInfo ChanageOrdersInfo2DeliveryOrdersInfo(DataBase dataBase, DbTransaction tran, string orderID, string vDeliveryOrderID, DeliveryOrderInfo deliveryOrderInfo)
        {
            int lineID = 0;
            IOrdersDA ordersDA = DataAccess.CreateOrdersDA();
            IOrderDetailDA orderDetailDA = DataAccess.CreateOrderDetailDA();
            OrdersInfo ordersInfo = ordersDA.GetOrdersByID(dataBase, tran, orderID, this.dataBaseID[0], this.pubParasInfo);
            List<OrderDetailInfo> orderDetailInfos = null;
            List<DeliveryOrderDetailInfo> TempDeliveryOrderDetailInfos = null;
            if (ordersInfo != null)
            {
                int i;
                if ((deliveryOrderInfo != null) && (deliveryOrderInfo.DeliveryOrderDetailInfos != null))
                {
                    lineID = deliveryOrderInfo.DeliveryOrderDetailInfos.Count + 1;
                }
                else
                {
                    lineID = 1;
                }
                deliveryOrderInfo.OwnerID = ordersInfo.OwnerID;
                deliveryOrderInfo.RequestedShipDate = ordersInfo.RequestedShipDate;
                deliveryOrderInfo.TransferType = ordersInfo.TransportationMode;
                deliveryOrderInfo.CarrierID = ordersInfo.CarrierID;
                deliveryOrderInfo.CustomerID = ordersInfo.ConsigneeID;
                deliveryOrderInfo.ActualShipDate = ordersInfo.ActualShipDate;
                deliveryOrderInfo.ExpectArrivalDate = DateTime.Now;
                deliveryOrderInfo.DeliveryOrderType = "Indoors";
                deliveryOrderInfo.StartLocAddr = "";
                deliveryOrderInfo.EndLocAddr = "";
                deliveryOrderInfo.Departure = "";
                deliveryOrderInfo.Destination = "";
                deliveryOrderInfo.Status = "New";
                deliveryOrderInfo.PickupType = "SelfPickUp";
                deliveryOrderInfo.PaymentType = "ByMonth";
                deliveryOrderInfo.BillingType = "ByWeight";
                deliveryOrderInfo.PickupLocID = "";
                deliveryOrderInfo.DeliveryLocID = "";
                deliveryOrderInfo.IsActive = "Y";
                deliveryOrderInfo.CreatedBy = this.pubParasInfo.UserID;
                deliveryOrderInfo.UpdatedBy = this.pubParasInfo.UserID;
                orderDetailInfos = orderDetailDA.GetOrderDetailListByOrderID(dataBase, tran, orderID, this.dataBaseID[0], this.pubParasInfo);
                if ((orderDetailInfos != null) && (orderDetailInfos.Count > 0))
                {
                    TempDeliveryOrderDetailInfos = new List<DeliveryOrderDetailInfo>();
                    for (i = 0; i < orderDetailInfos.Count; i++)
                    {
                        DeliveryOrderDetailInfo tempDeliveryOrderDetailInfo = new DeliveryOrderDetailInfo {
                            LineID = lineID * 10,
                            DeliveryOrderID = vDeliveryOrderID,
                            OwnerID = orderDetailInfos[i].OwnerID,
                            SkuID = orderDetailInfos[i].SkuID,
                            Status = "New",
                            PackID = orderDetailInfos[i].PackID,
                            UomID = orderDetailInfos[i].UomID,
                            OriginalQty = orderDetailInfos[i].OriginalQty,
                            RejectedQty = 0.0M,
                            CreatedBy = this.pubParasInfo.UserID,
                            CreatedDate = DateTime.Now,
                            UpdatedBy = this.pubParasInfo.UserID,
                            UpdatedDate = DateTime.Now,
                            IsActive = "Y",
                            Udf1 = orderDetailInfos[i].Udf1,
                            Udf2 = orderDetailInfos[i].Udf2,
                            Udf3 = orderDetailInfos[i].Udf3,
                            Udf4 = orderDetailInfos[i].Udf4,
                            Udf5 = orderDetailInfos[i].Udf5,
                            Udf6 = orderDetailInfos[i].Udf6,
                            Udf7 = orderDetailInfos[i].Udf7,
                            Udf8 = orderDetailInfos[i].Udf8,
                            Udf9 = orderDetailInfos[i].Udf9,
                            Udf10 = orderDetailInfos[i].Udf10,
                            Remark = orderDetailInfos[i].Remark
                        };
                        lineID++;
                        TempDeliveryOrderDetailInfos.Add(tempDeliveryOrderDetailInfo);
                    }
                }
                if (((TempDeliveryOrderDetailInfos != null) && (TempDeliveryOrderDetailInfos.Count > 0)) && (deliveryOrderInfo.DeliveryOrderDetailInfos != null))
                {
                    for (i = 0; i < TempDeliveryOrderDetailInfos.Count; i++)
                    {
                        if (deliveryOrderInfo.DeliveryOrderDetailInfos != null)
                        {
                            deliveryOrderInfo.DeliveryOrderDetailInfos.Add(TempDeliveryOrderDetailInfos[i]);
                        }
                    }
                    return deliveryOrderInfo;
                }
                deliveryOrderInfo.DeliveryOrderDetailInfos = TempDeliveryOrderDetailInfos;
            }
            return deliveryOrderInfo;
        }

        public string Delete(List<string> IDS)
        {
            string temp = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                List<DeliveryOrderInfo> deliveryOrderInfos = new List<DeliveryOrderInfo>();
                DeliveryOrderInfo deliveryOrderInfo = null;
                foreach (string id in IDS)
                {
                    deliveryOrderInfo = deliveryOrderDA.GetDeliveryOrderByID(dataBase, tran, id, this.dataBaseID[0], this.pubParasInfo);
                    deliveryOrderInfo.DeliveryOrderDetailInfos = deliveryOrderDetailDA.GetDeliveryOrderDetailListByID(dataBase, tran, id, this.dataBaseID[0], this.pubParasInfo);
                    deliveryOrderInfos.Add(deliveryOrderInfo);
                }
                AuditDataBLL auditDataBLL = new AuditDataBLL();
                deliveryOrderDetailDA.DeleteDeliveryOrderDetail(dataBase, tran, IDS, this.dataBaseID);
                deliveryOrderDA.DeleteDeliveryOrder(dataBase, tran, IDS, this.dataBaseID);
                for (int i = 0; i < IDS.Count; i++)
                {
                    auditDataBLL.InsertAuditData<DeliveryOrderInfo>(dataBase, tran, deliveryOrderInfos[i], null, AuditAction.Delete, this.pubParasInfo.UserID, "DELIVERY_ORDER", this.dataBaseID[0]);
                    if ((deliveryOrderInfos[i].DeliveryOrderDetailInfos != null) && (deliveryOrderInfos[i].DeliveryOrderDetailInfos.Count > 0))
                    {
                        for (int j = 0; j < deliveryOrderInfos[i].DeliveryOrderDetailInfos.Count; j++)
                        {
                            auditDataBLL.InsertAuditData<DeliveryOrderDetailInfo>(dataBase, tran, deliveryOrderInfos[i].DeliveryOrderDetailInfos[j], null, AuditAction.Delete, this.pubParasInfo.UserID, "DELIVERY_ORDER_DETAIL", this.dataBaseID[0]);
                        }
                    }
                    temp = temp + IDS[i] + ",";
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            if (temp.Length > 0)
            {
                return temp.Substring(0, temp.Length - 1);
            }
            return "";
        }

        public DataSet GetAllInfo()
        {
            return deliveryOrderDA.GetAllDeliveryOrder(this.dataBaseID[0], this.pubParasInfo);
        }

        public DeliveryOrderInfo GetDeliveryOrderInfoByOrderIDDocType(string orderID, string docType)
        {
            OrdersInfo ordersInfo;
            int i;
            DeliveryOrderDetailInfo deliveryOrderDetailInfo;
            DeliveryOrderInfo deliveryOrderInfo = new DeliveryOrderInfo();
            List<DeliveryOrderDetailInfo> deliveryOrderDetailList = new List<DeliveryOrderDetailInfo>();
            if (docType == "ORDER")
            {
                ordersInfo = ordersDA.GetOrdersByID(orderID, this.dataBaseID[0], this.pubParasInfo);
                if (ordersInfo == null)
                {
                    throw new Exception("此发货订单:" + orderID + "不存在!");
                }
                if (!(ordersInfo.Status == "Shipped"))
                {
                    throw new Exception("此发货订单:" + orderID + "没有发货完成!");
                }
                ordersInfo.OrderDetailInfos = orderDetailDA.GetOrderDetailListByOrderID(orderID, this.dataBaseID[0], this.pubParasInfo);
                if ((ordersInfo.OrderDetailInfos != null) && (ordersInfo.OrderDetailInfos.Count > 0))
                {
                    deliveryOrderInfo.ExternOrderID = ordersInfo.OrderID;
                    deliveryOrderInfo.RequestedShipDate = ordersInfo.RequestedShipDate;
                    deliveryOrderInfo.TransferType = ordersInfo.TransportationMode;
                    deliveryOrderInfo.CarrierID = ordersInfo.CarrierID;
                    deliveryOrderInfo.Status = "New";
                    deliveryOrderInfo.CustomerID = ordersInfo.ConsigneeID;
                    i = 1;
                    foreach (OrderDetailInfo info in ordersInfo.OrderDetailInfos)
                    {
                        deliveryOrderDetailInfo = new DeliveryOrderDetailInfo {
                            LineID = i * 10,
                            ExternOrderID = info.OrderID,
                            ExternOrderLineID = info.LineID.ToString(),
                            OwnerID = info.OwnerID,
                            SkuID = info.SkuID,
                            Status = "New",
                            PackID = info.PackID,
                            UomID = info.UomID,
                            OriginalQty = info.ShippedQty,
                            IsActive = "Y"
                        };
                        i++;
                        deliveryOrderDetailList.Add(deliveryOrderDetailInfo);
                    }
                    deliveryOrderInfo.DeliveryOrderDetailInfos = deliveryOrderDetailList;
                }
                return deliveryOrderInfo;
            }
            WaveInfo waveInfo = waveDA.GetWaveByID(orderID, this.dataBaseID[0], this.pubParasInfo);
            if (waveInfo == null)
            {
                throw new Exception("此批量订单:" + orderID + "不存在!");
            }
            if (!(waveInfo.Status == "Shipped"))
            {
                throw new Exception("此批量订单:" + orderID + "没有发货完成!");
            }
            waveInfo.WaveDetailInfo = waveDetailDA.GetWaveDetailListByID(orderID, this.dataBaseID[0]);
            if ((waveInfo.WaveDetailInfo != null) && (waveInfo.WaveDetailInfo.Count > 0))
            {
                deliveryOrderInfo.ExternOrderID = orderID;
                ordersInfo = ordersDA.GetOrdersByID(waveInfo.WaveDetailInfo[0].OrderID, this.dataBaseID[0], this.pubParasInfo);
                if (ordersInfo != null)
                {
                    deliveryOrderInfo.TransferType = ordersInfo.TransportationMode;
                    deliveryOrderInfo.RequestedShipDate = ordersInfo.RequestedShipDate;
                    deliveryOrderInfo.CarrierID = ordersInfo.CarrierID;
                    deliveryOrderInfo.CustomerID = ordersInfo.ConsigneeID;
                }
                i = 1;
                foreach (WaveDetailInfo info in waveInfo.WaveDetailInfo)
                {
                    OrdersInfo orderInfo = ordersDA.GetOrdersByID(info.OrderID, this.dataBaseID[0], this.pubParasInfo);
                    if ((!(orderInfo.OwnerID == ordersInfo.OwnerID) || !(orderInfo.CarrierID == ordersInfo.CarrierID)) || !(orderInfo.ConsigneeID == ordersInfo.ConsigneeID))
                    {
                        throw new Exception("此批量订单:" + orderID + "的货主、承运商、客户等信息不一致!");
                    }
                    orderInfo.OrderDetailInfos = orderDetailDA.GetOrderDetailListByOrderID(info.OrderID, this.dataBaseID[0], this.pubParasInfo);
                    foreach (OrderDetailInfo ODI in orderInfo.OrderDetailInfos)
                    {
                        deliveryOrderDetailInfo = new DeliveryOrderDetailInfo {
                            LineID = i * 10,
                            ExternOrderID = ODI.OrderID,
                            ExternOrderLineID = ODI.LineID.ToString(),
                            OwnerID = ODI.OwnerID,
                            SkuID = ODI.SkuID,
                            Status = "New",
                            PackID = ODI.PackID,
                            UomID = ODI.UomID,
                            OriginalQty = ODI.ShippedQty,
                            IsActive = "Y"
                        };
                        i++;
                        deliveryOrderDetailList.Add(deliveryOrderDetailInfo);
                    }
                }
                deliveryOrderInfo.DeliveryOrderDetailInfos = deliveryOrderDetailList;
            }
            return deliveryOrderInfo;
        }

        public string GetDeliveryOrderStatus(List<DeliveryOrderDetailInfo> deliveryOrderDetailInfos)
        {
            return "";
        }

        public DeliveryOrderInfo GetInfoByID(string id)
        {
            DeliveryOrderInfo deliveryOrderInfo = deliveryOrderDA.GetDeliveryOrderByID(id, this.dataBaseID[0], this.pubParasInfo);
            if (deliveryOrderInfo != null)
            {
                deliveryOrderInfo.DeliveryOrderDetailInfos = deliveryOrderDetailDA.GetDeliveryOrderDetailListByID(deliveryOrderInfo.DeliveryOrderID, this.dataBaseID[0], this.pubParasInfo);
            }
            return deliveryOrderInfo;
        }

        public DataSet GetInfoByQueryEntity(object obj)
        {
            DeliveryOrderQueryEntity deliveryOrderQuery = obj as DeliveryOrderQueryEntity;
            List<string> sqlWhere = new List<string>();
            if (!string.IsNullOrEmpty(deliveryOrderQuery.DeliveryOrderID))
            {
                sqlWhere.Add(" DO.DELIVERY_ORDER_ID " + deliveryOrderQuery.DeliveryOrderID);
            }
            if (!string.IsNullOrEmpty(deliveryOrderQuery.DeliveryOrderType))
            {
                sqlWhere.Add(" DO.DELIVERY_ORDER_TYPE " + deliveryOrderQuery.DeliveryOrderType);
            }
            if (!string.IsNullOrEmpty(deliveryOrderQuery.StartLocAddr))
            {
                sqlWhere.Add(" DO.START_LOC_ADDR " + deliveryOrderQuery.StartLocAddr);
            }
            if (!string.IsNullOrEmpty(deliveryOrderQuery.EndLocAddr))
            {
                sqlWhere.Add(" DO.END_LOC_ADDR " + deliveryOrderQuery.EndLocAddr);
            }
            if (!string.IsNullOrEmpty(deliveryOrderQuery.Departure))
            {
                sqlWhere.Add(" DO.DEPARTURE " + deliveryOrderQuery.Departure);
            }
            if (!string.IsNullOrEmpty(deliveryOrderQuery.Destination))
            {
                sqlWhere.Add(" DO.DESTINATION " + deliveryOrderQuery.Destination);
            }
            if (!string.IsNullOrEmpty(deliveryOrderQuery.RequestedShipDate))
            {
                sqlWhere.Add(" DO.REQUESTED_SHIP_DATE " + deliveryOrderQuery.RequestedShipDate);
            }
            if (!string.IsNullOrEmpty(deliveryOrderQuery.ActualShipDate))
            {
                sqlWhere.Add(" DO.ACTUAL_SHIP_DATE " + deliveryOrderQuery.ActualShipDate);
            }
            if (!string.IsNullOrEmpty(deliveryOrderQuery.ExpectArrivalDate))
            {
                sqlWhere.Add(" DO.EXPECT_ARRIVAL_DATE " + deliveryOrderQuery.ExpectArrivalDate);
            }
            if (!string.IsNullOrEmpty(deliveryOrderQuery.Status))
            {
                sqlWhere.Add(" DO.STATUS " + deliveryOrderQuery.Status);
            }
            if (!string.IsNullOrEmpty(deliveryOrderQuery.TransferType))
            {
                sqlWhere.Add(" DO.TRANSFER_TYPE " + deliveryOrderQuery.TransferType);
            }
            if (!string.IsNullOrEmpty(deliveryOrderQuery.PickupType))
            {
                sqlWhere.Add(" DO.PICKUP_TYPE " + deliveryOrderQuery.PickupType);
            }
            if (!string.IsNullOrEmpty(deliveryOrderQuery.PaymentType))
            {
                sqlWhere.Add(" DO.PAYMENT_TYPE " + deliveryOrderQuery.PaymentType);
            }
            if (!string.IsNullOrEmpty(deliveryOrderQuery.CustomerID))
            {
                sqlWhere.Add(" DO.CUSTOMER_ID " + deliveryOrderQuery.CustomerID);
            }
            if (!string.IsNullOrEmpty(deliveryOrderQuery.PickupLocID))
            {
                sqlWhere.Add(" DO.PICKUP_LOC_ID " + deliveryOrderQuery.PickupLocID);
            }
            if (!string.IsNullOrEmpty(deliveryOrderQuery.DeliveryLocID))
            {
                sqlWhere.Add(" DO.DELIVERY_LOC_ID " + deliveryOrderQuery.DeliveryLocID);
            }
            if (!string.IsNullOrEmpty(deliveryOrderQuery.IsActive))
            {
                sqlWhere.Add(" DO.IS_ACTIVE " + deliveryOrderQuery.IsActive);
            }
            if (!string.IsNullOrEmpty(deliveryOrderQuery.CreatedBy))
            {
                sqlWhere.Add(" DO.CREATED_BY " + deliveryOrderQuery.CreatedBy);
            }
            if (!string.IsNullOrEmpty(deliveryOrderQuery.DocType))
            {
                sqlWhere.Add(" DO.DOC_TYPE " + deliveryOrderQuery.DocType);
            }
            if (!string.IsNullOrEmpty(deliveryOrderQuery.ExternOrderID))
            {
                sqlWhere.Add(" DO.EXTERN_ORDER_ID " + deliveryOrderQuery.ExternOrderID);
            }
            if (!string.IsNullOrEmpty(deliveryOrderQuery.ExternOrderId2))
            {
                sqlWhere.Add(" DO.EXTERN_ORDER_ID2 " + deliveryOrderQuery.ExternOrderId2);
            }
            return deliveryOrderDA.GetDeliveryOrderByQueryList(sqlWhere, deliveryOrderQuery, this.dataBaseID[0], this.pubParasInfo);
        }

        public string ImportFormShippingOrder(string orderID, string vDeliveryOrderID)
        {
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string deliveryOrderID = string.Empty;
            try
            {
                int i;
                DeliveryOrderInfo deliveryOrderInfo = null;
                if (string.IsNullOrEmpty(vDeliveryOrderID))
                {
                    deliveryOrderInfo = new DeliveryOrderInfo();
                    vDeliveryOrderID = commonMethod.GenNextNumStr(dataBase, tran, "DELIVERY_ORDER", this.dataBaseID[0]);
                    deliveryOrderID = vDeliveryOrderID;
                    deliveryOrderInfo = this.ChanageOrdersInfo2DeliveryOrdersInfo(dataBase, tran, orderID, vDeliveryOrderID, deliveryOrderInfo);
                    deliveryOrderInfo.DeliveryOrderID = vDeliveryOrderID;
                    deliveryOrderInfo.ExternOrderID = orderID;
                    deliveryOrderInfo.RequestedShipDate = DateTime.Now;
                    deliveryOrderInfo.ActualShipDate = DateTime.Now;
                    deliveryOrderInfo.ExpectArrivalDate = DateTime.Now;
                    deliveryOrderInfo.EntrustDate = DateTime.Now;
                    deliveryOrderInfo.Receipted = "N";
                    deliveryOrderInfo.CreatedBy = this.pubParasInfo.UserID;
                    deliveryOrderInfo.CreatedDate = DateTime.Now;
                    deliveryOrderInfo.UpdatedBy = this.pubParasInfo.UserID;
                    deliveryOrderInfo.UpdatedDate = DateTime.Now;
                    deliveryOrderDA.InsertDeliveryOrder(dataBase, tran, deliveryOrderInfo, this.dataBaseID);
                    if ((deliveryOrderInfo.DeliveryOrderDetailInfos != null) && (deliveryOrderInfo.DeliveryOrderDetailInfos.Count > 0))
                    {
                        for (i = 0; i < deliveryOrderInfo.DeliveryOrderDetailInfos.Count; i++)
                        {
                            deliveryOrderDetailDA.InsertDeliveryOrderDetail(dataBase, tran, deliveryOrderInfo.DeliveryOrderDetailInfos[i], this.dataBaseID);
                        }
                    }
                }
                else
                {
                    deliveryOrderInfo = deliveryOrderDA.GetDeliveryOrderByID(vDeliveryOrderID, this.dataBaseID[0], this.pubParasInfo);
                    deliveryOrderID = deliveryOrderInfo.DeliveryOrderID;
                    deliveryOrderInfo = this.ChanageOrdersInfo2DeliveryOrdersInfo(dataBase, tran, orderID, vDeliveryOrderID, deliveryOrderInfo);
                    deliveryOrderDA.UpdateDeliveryOrder(dataBase, tran, deliveryOrderInfo, this.dataBaseID);
                    if ((deliveryOrderInfo.DeliveryOrderDetailInfos != null) && (deliveryOrderInfo.DeliveryOrderDetailInfos.Count > 0))
                    {
                        for (i = 0; i < deliveryOrderInfo.DeliveryOrderDetailInfos.Count; i++)
                        {
                            deliveryOrderDetailDA.UpdateDeliveryOrderDetail(dataBase, tran, deliveryOrderInfo.DeliveryOrderDetailInfos[i], this.dataBaseID);
                        }
                    }
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return deliveryOrderID;
        }

        public string Insert(object obj)
        {
            string deliveryOrderID = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                DeliveryOrderInfo deliveryOrderInfo = obj as DeliveryOrderInfo;
                if (!string.IsNullOrEmpty(deliveryOrderInfo.CustomerID))
                {
                    ICustomerDA customerDA = DataAccess.CreateCustomerDA();
                    if (!customerDA.CheckCustomerIDUnique(dataBase, tran, deliveryOrderInfo.CustomerID, this.dataBaseID[0]))
                    {
                        PublicMethod.GetInstance().ShowAlertMessage("F8100_001", this.pubParasInfo.UserLanguage);
                    }
                    else if (!customerDA.CheckAuthorizationCustomerIDUserID(dataBase, tran, deliveryOrderInfo.CustomerID, this.dataBaseID[0], this.pubParasInfo))
                    {
                        PublicMethod.GetInstance().ShowAlertMessage("F2120_002", this.pubParasInfo.UserLanguage, new List<string> { deliveryOrderInfo.CustomerID });
                    }
                }
                if (!string.IsNullOrEmpty(deliveryOrderInfo.PickupLocID) && !DataAccess.CreateLocAddrDA().CheckLocAddrIDUnique(dataBase, tran, deliveryOrderInfo.PickupLocID, this.dataBaseID[0]))
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F8100_002", this.pubParasInfo.UserLanguage);
                }
                if (!string.IsNullOrEmpty(deliveryOrderInfo.DeliveryLocID) && !DataAccess.CreateLocAddrDA().CheckLocAddrIDUnique(dataBase, tran, deliveryOrderInfo.DeliveryLocID, this.dataBaseID[0]))
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F8100_003", this.pubParasInfo.UserLanguage);
                }
                if (!string.IsNullOrEmpty(deliveryOrderInfo.DeliveryOrderID))
                {
                    if (deliveryOrderDA.CheckDeliveryOrderIDUnique(dataBase, tran, deliveryOrderInfo.DeliveryOrderID, this.dataBaseID[0]))
                    {
                        PublicMethod.GetInstance().ShowAlertMessage("F8100_004", this.pubParasInfo.UserLanguage, new List<string> { deliveryOrderInfo.DeliveryOrderID });
                    }
                }
                else
                {
                    deliveryOrderInfo.DeliveryOrderID = commonMethod.GenNextNumStr(dataBase, tran, "DELIVERY_ORDER", this.dataBaseID[0]);
                }
                deliveryOrderInfo.UpdatedBy = deliveryOrderInfo.CreatedBy;
                deliveryOrderInfo.CreatedDate = DateTime.Now;
                deliveryOrderInfo.UpdatedDate = DateTime.Now;
                deliveryOrderDA.InsertDeliveryOrder(dataBase, tran, deliveryOrderInfo, this.dataBaseID);
                AuditDataBLL auditDataBLL = new AuditDataBLL();
                auditDataBLL.InsertAuditData<DeliveryOrderInfo>(dataBase, tran, null, deliveryOrderInfo, AuditAction.Insert, this.pubParasInfo.UserID, "DELIVERY_ORDER", this.dataBaseID[0]);
                deliveryOrderID = deliveryOrderInfo.DeliveryOrderID;
                if ((deliveryOrderInfo.DeliveryOrderDetailInfos != null) && (deliveryOrderInfo.DeliveryOrderDetailInfos.Count > 0))
                {
                    for (int i = 0; i < deliveryOrderInfo.DeliveryOrderDetailInfos.Count; i++)
                    {
                        deliveryOrderInfo.DeliveryOrderDetailInfos[i].DeliveryOrderID = deliveryOrderID;
                        if (deliveryOrderDetailDA.CheckDeliveryOrderDetailIDUnique(dataBase, tran, deliveryOrderInfo.DeliveryOrderDetailInfos[i].DeliveryOrderID, deliveryOrderInfo.DeliveryOrderDetailInfos[i].LineID.ToString(), this.dataBaseID[0]))
                        {
                            throw new Exception(string.Concat(new object[] { "DeliveryOrderID:", deliveryOrderInfo.DeliveryOrderDetailInfos[i].DeliveryOrderID, "',LineID:", deliveryOrderInfo.DeliveryOrderDetailInfos[i].LineID, "'已经存在! 请重新输入!" }));
                        }
                        deliveryOrderInfo.DeliveryOrderDetailInfos[i].DOD_Status = "";
                        deliveryOrderInfo.DeliveryOrderDetailInfos[i].Status = "New";
                        deliveryOrderInfo.DeliveryOrderDetailInfos[i].CreatedBy = deliveryOrderInfo.CreatedBy;
                        deliveryOrderInfo.DeliveryOrderDetailInfos[i].UpdatedBy = deliveryOrderInfo.UpdatedBy;
                        deliveryOrderInfo.DeliveryOrderDetailInfos[i].CreatedDate = DateTime.Now;
                        deliveryOrderInfo.DeliveryOrderDetailInfos[i].UpdatedDate = DateTime.Now;
                        deliveryOrderDetailDA.InsertDeliveryOrderDetail(dataBase, tran, deliveryOrderInfo.DeliveryOrderDetailInfos[i], this.dataBaseID);
                        auditDataBLL.InsertAuditData<DeliveryOrderDetailInfo>(dataBase, tran, null, deliveryOrderInfo.DeliveryOrderDetailInfos[i], AuditAction.Insert, this.pubParasInfo.UserID, "DELIVERY_ORDER_DETAIL", this.dataBaseID[0]);
                    }
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return deliveryOrderID;
        }

        public string PODDeliveryOrderStatus(DeliveryOrderInfo deliveryOrderInfo)
        {
            string result = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                deliveryOrderInfo.UpdatedBy = this.pubParasInfo.UserID;
                if (deliveryOrderDA.UpdateDeliveryOrderPOD(dataBase, tran, deliveryOrderInfo, this.dataBaseID[0]) > 0)
                {
                    result = deliveryOrderInfo.DeliveryOrderID;
                }
                if (deliveryOrderDetailDA.UpdateUpdateDeliveryOrderDetailPOD(dataBase, tran, deliveryOrderInfo.DeliveryOrderDetailInfos, this.pubParasInfo.UserID, this.dataBaseID[0]) > 0)
                {
                }
                tran.Commit();
                SysSettingInfo sysSettinginfo = sysSettingDA.GetSysSettingByID("CEAWebService", this.dataBaseID[0], this.pubParasInfo);
                if ((sysSettinginfo != null) && (sysSettinginfo.Value == "Y"))
                {
                    new CEABLL(this.pubParasInfo).SignForFeedbackInformation(deliveryOrderInfo);
                }
                return result;
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public string Update(object obj)
        {
            string deliveryOrderID = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                DeliveryOrderInfo deliveryOrderInfo = obj as DeliveryOrderInfo;
                if (!string.IsNullOrEmpty(deliveryOrderInfo.CustomerID))
                {
                    ICustomerDA customerDA = DataAccess.CreateCustomerDA();
                    if (!customerDA.CheckCustomerIDUnique(dataBase, tran, deliveryOrderInfo.CustomerID, this.dataBaseID[0]))
                    {
                        PublicMethod.GetInstance().ShowAlertMessage("F8100_001", this.pubParasInfo.UserLanguage);
                    }
                    else if (!customerDA.CheckAuthorizationCustomerIDUserID(dataBase, tran, deliveryOrderInfo.CustomerID, this.dataBaseID[0], this.pubParasInfo))
                    {
                        PublicMethod.GetInstance().ShowAlertMessage("F2120_002", this.pubParasInfo.UserLanguage, new List<string> { deliveryOrderInfo.CustomerID });
                    }
                }
                if (!string.IsNullOrEmpty(deliveryOrderInfo.PickupLocID) && !DataAccess.CreateLocAddrDA().CheckLocAddrIDUnique(dataBase, tran, deliveryOrderInfo.PickupLocID, this.dataBaseID[0]))
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F8100_002", this.pubParasInfo.UserLanguage);
                }
                if (!string.IsNullOrEmpty(deliveryOrderInfo.DeliveryLocID) && !DataAccess.CreateLocAddrDA().CheckLocAddrIDUnique(dataBase, tran, deliveryOrderInfo.DeliveryLocID, this.dataBaseID[0]))
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F8100_003", this.pubParasInfo.UserLanguage);
                }
                deliveryOrderInfo.UpdatedDate = DateTime.Now;
                DeliveryOrderInfo oldDeliveryOrderInfo = deliveryOrderDA.GetDeliveryOrderByID(dataBase, tran, deliveryOrderInfo.DeliveryOrderID, this.dataBaseID[0], this.pubParasInfo);
                if (deliveryOrderDA.UpdateDeliveryOrder(deliveryOrderInfo, this.dataBaseID) > 0)
                {
                    new AuditDataBLL().InsertAuditData<DeliveryOrderInfo>(oldDeliveryOrderInfo, deliveryOrderInfo, AuditAction.Update, this.pubParasInfo.UserID, "DELIVERY_ORDER", this.dataBaseID[0]);
                    deliveryOrderID = deliveryOrderInfo.DeliveryOrderID;
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return deliveryOrderID;
        }
    }
}

